package com.itheima.thread.synchronizedsafety;

public class Account {
    // 卡号
    private String cardId;
    // 余额
    private double balance;

    public Account(){

    }
    public Account(String cardId, double balance){
        this.cardId = cardId;
        this.balance = balance;
    }

    public void setcardId(String cardId){
        this.cardId = cardId;
    }

    public String getcardId(){
        return this.cardId;
    }

    public void setBalance(double balance){
        this.balance = balance;
    }

    public double getBalance(){
        return this.balance;
    }


    //取款
    public void withdraw(double money){

        // 同步代码块
        synchronized (this){
            //取款前的余额
            double beforeMoney =getBalance();
            //取款后的余额
            double afterMoney = beforeMoney - money;
            //更新余额
            this.setBalance(afterMoney);
        }


    }



}
